package com.abe.libquick.main.video.record;

import android.animation.Animator;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.View;

import com.abe.libcore.base.screen.OnHandlerListener;
import com.abe.libcore.utils.FileUtils;
import com.abe.libquick.R;
import com.abe.libquick.R2;
import com.abe.libquick.main.video.player.VideoPreviewActivity;
import com.abe.libquick.main.video.player.VideoRecordHelper;
import com.abe.libquick.utils.quick.EmptyUtils;

import butterknife.OnClick;

public class VideoRecordPreviewActivity extends VideoBaseActivity implements SurfaceHolder.Callback, Animator.AnimatorListener,
        MediaRecorder.OnErrorListener, OnHandlerListener {
    private static final int REQUEST_PREVIEW = 0x999;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_video_record_preview;
    }

    @OnClick(R2.id.item_close)
    public void close() {
        setResultCancel();
    }

    /**
     * 回调方法，从预览页面回来的时候会执行这个方法
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_PREVIEW) {
            if (EmptyUtils.isEmpty(data) || EmptyUtils.isEmpty(data.getExtras())) {
                animator_normal.start();
            } else {
                Bundle bundle = data.getExtras();
                if (bundle.containsKey(VideoRecordHelper.VIDEO_CONFIRM)) {
                    boolean confirm = bundle.getBoolean(VideoRecordHelper.VIDEO_CONFIRM, false);
                    if (confirm) {
                        String path = bundle.getString(VideoRecordHelper.VIDEO_PATH);
                        Bundle mBundle = new Bundle();
                        mBundle.putString(VideoRecordHelper.VIDEO_PATH, path);
                        setResultOK(mBundle);
                    } else {
                        animator_normal.start();
                    }
                } else {
                    animator_normal.start();
                }
            }
        }
    }

    @Override
    public void dispatchHandleMessage(Message msg) {
        switch (msg.what) {
            case REFRESH_PROGRESS:
                int progress = (int) ((float) count * 100 / (float) maxVideoLength);
                /* 刷新进度条 */
                if (progress == 0) {
                    progressBar.setProgress(0);
                } else {
                    progressBar.setProgressWithAnimation(progress, (long) 500);
                }
                break;
            case STOP_RECORD: {
                if (count < 15) {
                    animator_normal.start();
                } else {
                    /* 初始化菜单 */
                    progressBar.setProgress(0);
                    control.setBackgroundResource(R.color.translate);
                    control.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.INVISIBLE);
                    btnStartStop.setVisibility(View.VISIBLE);
                    //前往预览
                    Bundle bundle = new Bundle();
                    bundle.putString(VideoRecordHelper.VIDEO_PATH, currentVideo);
                    gotoActivityForResult(VideoPreviewActivity.class, bundle, REQUEST_PREVIEW);
                }
            }
            break;
            case RESET_RECORD: {
                /* 清除录制缓存 */
                FileUtils.INSTANCE.deleteFile(currentVideo);
                currentVideo = "";
                /* 初始化菜单 */
                progressBar.setProgress(0);
                control.setBackgroundResource(R.color.translate);
                control.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.INVISIBLE);
                btnStartStop.setVisibility(View.VISIBLE);
                if (mSurfaceHolder != null)
                    surfaceCreated(mSurfaceHolder);
            }
            break;
        }
    }
}